package com.elcl.util.viewutils;

import android.graphics.drawable.Drawable;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.TextAppearanceSpan;
import android.widget.TextView;

import com.elcl.storage.ApplicationCache;

public class TextViewUtils {

	public static final int GravityTop = 0;
	public static final int GravityLeft = 1;
	public static final int GravityRight = 2;
	public static final int GravityBottom = 3;

	/**
	 * 设置TextView中的周围的图片（当在代码中设置drawableLeft的时候）
	 */
	public static void setTextDrawableAround(TextView txt_with_drawable,
			int resId, int Gravity) {
		Drawable drawable = ApplicationCache.context.getResources()
				.getDrawable(resId);
		// / 这一步必须要做,否则不会显示.
		drawable.setBounds(0, 0, drawable.getMinimumWidth(),
				drawable.getMinimumHeight());
		if (Gravity == GravityLeft) {
			txt_with_drawable.setCompoundDrawables(drawable, null, null, null);
		} else if (Gravity == GravityTop) {
			txt_with_drawable.setCompoundDrawables(null, drawable, null, null);
		} else if (Gravity == GravityRight) {
			txt_with_drawable.setCompoundDrawables(null, null, drawable, null);
		} else if (Gravity == GravityBottom) {
			txt_with_drawable.setCompoundDrawables(null, null, null, drawable);
		}
	}

	/**
	 * @param txt_input
	 *            (要改变的TextView)
	 * @param colors
	 *            ，设置字体的不同颜色
	 *  spannable设置间距
	 *            ，在哪些地方要分隔开 用于一行字体中，显示不同的颜色
	 */
	public static void setTextWithDiffentColor(TextView txt_input,
			int[] colors, int[] spannable) {
		String str_input = ViewUtils.getStringOfView(txt_input);
		SpannableString styledText = new SpannableString(str_input);
		styledText.setSpan(new TextAppearanceSpan(ApplicationCache.context,
				colors[0]), 0, spannable[0], Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
		styledText.setSpan(new TextAppearanceSpan(ApplicationCache.context,
				colors[1]), spannable[0], spannable[1],
				Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

		txt_input.setText(styledText, TextView.BufferType.SPANNABLE);
	}
}
